home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / des / getpass.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  1KB  |  51 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sgtty.h>
  4.  
  5. #define    TTY    "/dev/tty"    /* Change to "con" for MS-DOS */
  6.  
  7. #define    SIG    void            /* Change to run on sun */
  8.  
  9. /* Issue prompt and read reply with echo turned off */
  10. char *
  11. getpass(prompt)
  12. char *prompt;
  13. {
  14.     struct sgttyb ttyb,ttysav;
  15.     register char *cp;
  16.     int c;
  17.     FILE *tty;
  18.     static char pbuf[128];
  19.     SIG (*signal())(), (*sig)();    /* change for sun */
  20. /*    int (*signal())(), (*sig)(); */ /* orginal */
  21.  
  22.     if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
  23.         tty = stdin;
  24.     else
  25.         setbuf(tty, (char *)NULL);
  26.     sig = signal(SIGINT, SIG_IGN);
  27.     ioctl(fileno(tty), TIOCGETP, &ttyb);
  28.     ioctl(fileno(tty), TIOCGETP, &ttysav);
  29.     ttyb.sg_flags |= RAW;
  30.     ttyb.sg_flags &= ~ECHO;
  31.     ioctl(fileno(tty), TIOCSETP, &ttyb);
  32.     fprintf(stderr, "%s", prompt);
  33.     fflush(stderr);
  34.     cp = pbuf;
  35.     for (;;) {
  36.         c = getc(tty);
  37.         if(c == '\r' || c == '\n' || c == EOF)
  38.             break;
  39.         if (cp < &pbuf[127])
  40.             *cp++ = c;
  41.     }
  42.     *cp = '\0';
  43.     fprintf(stderr,"\r\n");
  44.     fflush(stderr);
  45.     ioctl(fileno(tty), TIOCSETP, &ttysav);
  46.     signal(SIGINT, sig);
  47.     if (tty != stdin)
  48.         fclose(tty);
  49.     return(pbuf);
  50. }
  51.